
const { query } = require('../db/mysql')

class SearchToken {
  constructor () {
    this.tableName = 'search_token'
  }

  // 根据分词名获取数据
  getByNames (arrNames = []) {
    if (arrNames.length === 0) {
      return []
    }

    const sql = `SELECT ${this.tableName}.id, ${this.tableName}.name
                FROM ${this.tableName} 
                WHERE name in(?) 
                limit ?`
    return query(sql, [arrNames, arrNames.length])
  }

  // 根据分词前缀获取数据
  getByPrefix (strPrefix) {
    const sql = `SELECT ${this.tableName}.name
                FROM ${this.tableName} 
                WHERE ${this.tableName}.name like ?
                ORDER BY char_length(${this.tableName}.name)
                limit ?`
    return query(sql, [strPrefix + '%', 10])
  }
}

module.exports = new SearchToken()
